# -*- coding:utf-8 -*-
from  . import app_register
from flask import render_template,request,flash,redirect,url_for
from app import db

class Users(db.Model): ##继承父类Model
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100))
    passwd = db.Column(db.String(100))
    def __init__(self, username, passwd):
        self.username = username
        self.passwd = passwd



@app_register.route("/")
def index():
    return render_template('register.html')

@app_register.route("/register_write_mysql",methods=['POST'])
def register_write_mysql():
    if request.method == 'POST':
        if not request.form['username'] or not request.form['passwd']:
            flash('输入有误')
        else:
            try:
                sql = Users(request.form['username'],request.form['passwd'])
                db.session.add(sql)
                db.session.commit()
                flash('创建用户成功')
            except Exception as e:
                flash('用户已存在')
    return redirect(url_for('app_register.index'))

